package base.view.ui

import android.content.Context
import android.util.AttributeSet
import android.widget.ScrollView

/**
 * Created by jjw on 2018/4/27 0027.
 */
class ProductDetailScrollView : ScrollView {
    var isTopEnd: Boolean = false
    private set
    var isBottomEnd: Boolean = false
        private set
    private var mScrollChangedListener: IScrollChangedListener? = null

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    fun setScrollViewListener(scrollViewListener: IScrollChangedListener) {
        this.mScrollChangedListener = scrollViewListener
    }

    interface IScrollChangedListener {
        fun onScrolledToBottom()
        fun onScrolledToTop()
    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        //super.onScrollChanged(l, t, oldl, oldt);
        val view = getChildAt(0) ?: return
        val diff = view.bottom - (height + scrollY)
        isBottomEnd = false
        isTopEnd = false

        if (diff == 0) {
            isBottomEnd = true
            if (mScrollChangedListener != null)
                mScrollChangedListener!!.onScrolledToBottom()
        } else if (scrollY == 0) {
            isTopEnd = true
            if (mScrollChangedListener != null)
                mScrollChangedListener!!.onScrolledToTop()
        }
    }
}